<?php
/*
 * @Create By: Vscode
 * @Author: falcon
 * @Date: 2021-02-12 20:42:44
 * 产品回收站和配件辅料回收站中“可用数量”为0的SKU（即是下架产品），每日凌晨3点自动清除货位数据一次。
 */
namespace application\timer;


use application\depend\Applog;
use application\model\PdoModel;

 class CleanCycle {

    public $pro = [];
    public $pro_acc = [];

    // 程序主入口
    public function run(){
        //首先找出 product 和 product_acc 表中 所有 status=2 && available_stock = 0 的 集合
        $this->collection('product');
        $this->collection('product_acc');

        // 清除掉成品仓库的sku
        if(!empty($this->pro) && is_array($this->pro)){
            // 获取所有成品仓库信息
            $warehouse_finished_products = $this->getWarehosues('finished');
            if(!empty($warehouse_finished_products)){
                $res = $this->clearWarehouse($warehouse_finished_products);
                if(!$res){
                    Applog::errorLog(['title'=>'清除成品仓库货架信息失败','date'=>date('Y-m-d H:i:s')]);
                }else{
                    Applog::errorLog(['title'=>'清除成品仓库货架信息成功','date'=>date('Y-m-d H:i:s')]);
                }

                // 删除回收站信息
                $this->deleCycleProductWhichIsZeroStocks();
            }// if empty warehouse
        }// if

        // 清除掉半成品仓库的sku
        if(!empty($this->pro_acc) && is_array($this->pro_acc)){
            $warehouse_semi_finished_products = $this->getWarehosues('semi');
            if(!empty($warehouse_semi_finished_products)){
                $res = $this->clearWarehouse($warehouse_semi_finished_products);
                if(!$res){
                    Applog::errorLog(['title'=>'清除半成品仓库货架信息失败','date'=>date('Y-m-d H:i:s')]);
                }else{
                    Applog::errorLog(['title'=>'清除半成品仓库货架信息成功','date'=>date('Y-m-d H:i:s')]);
                }
            }//if 
            
        } // if
    }// func

    // 收集那些在回收站且可用库存数=0的 sku名称
    public function collection($tables){
        $sql = "SELECT id,sku FROM {$tables} where status=2 and available_stock=0";
        $res = PdoModel::selectData($sql,[],"回收站且库存数为0；SKU类型：".$tables);
        if(false === $res){// 查询失败； 有可能是数据库挂了
            return;
        }// if

        if($tables === 'product_acc'){
            $this->pro_acc = $res;
            return;
        }// if

        $this->pro = $res;
    }// func

    // 获取仓库所有信息
    public function getWarehosues($product_status){
        if($product_status === 'finished'){ // 成品仓库
            $sql = "select wh_code from warehouse_manage where status=1 and type=1";
            $res = PdoModel::selectData($sql,[],"成品仓库所有信息");
            return $res;
        }// if

        $sql = "select * from warehouse_manage where status=1 and type=2"; // 半成品所有信息
        $res = PdoModel::selectData($sql,[],"半成品仓库所有信息");
        return $res;

    }// func

    // 清理仓库货架上为0的SKU
    public function clearWarehouse($warehouse_collections){
        $skuString = "";
        foreach($this->pro as $v){
            $skuString .= "'" . $v['sku'] . "',";
        }
        $skuString = rtrim($skuString,",");
        foreach($warehouse_collections as $v){
            $warehouse = 'warehouse'.$v['wh_code'];
            $sql = "select id from {$warehouse} where sku in ({$skuString}) and stocks=0";
            $result = PdoModel::selectData($sql,[],"查询对应SKU是否存在仓库中");
            
            if(!empty($result)){
                $sqldel = "delete from {$warehouse} where sku in ({$skuString}) and stocks=0";
                
                $i = [
                    'title' => '清理库存数为零且处于回收状态的SKU，为仓库腾出货位!',
                    'sql' => $sqldel,
                    'time' => date('Y-m-d H:i:s'),
                ];
                Applog::infoLog($i,__FILE__,__LINE__);
                if(false === PdoModel::selectData($sqldel,[],"清理库存数为零且处于回收状态的SKU，为仓库腾出货位!")){
                    return false;
                }
            }// if
        }// foreach

        return true;
    }// func

    // 删除回收站信息;  订单详情表 order_info 有sku记录的不能删， 会影响数据统计分析
    public function deleCycleProductWhichIsZeroStocks(){
        // 先检查订单详情中是否存在该sku 如果存在就不删除
        if(!empty($this->pro)){
            foreach($this->pro as $k=>$v){
                $sku = $v['sku'];
                $sql = "select id from order_info where sku='{$sku}' limit 1";
                $res = PdoModel::selectData($sql,[],'检查订单详情中是否有该SKU，有则去掉');
                if(!empty($res)) unset($this->pro[$k]);
            }
        }

        $this->pro = array_values($this->pro);

        if(!empty($this->pro_acc)){
            foreach($this->pro_acc as $k=>$v){
                $sku = $v['sku'];
                $sql = "select id from order_info where sku='{$sku}' limit 1";
                $res = PdoModel::selectData($sql,[],'检查订单详情中是否有该SKU，有则去掉acc');
                if(!empty($res)) unset($this->pro_acc[$k]);
            }
        }

        $this->pro_acc = array_values($this->pro_acc);

        //执行删除
        if(!empty($this->pro)){
            foreach($this->pro as $v){
                $id = $v['id'];
                $sql = "delete from product where id=$id limit 1";
                PdoModel::selectData($sql,[],'删除回收站零库存信息');
            }
        }

        if(!empty($this->pro_acc)){
            foreach($this->pro_acc as $v){
                $id = $v['id'];
                $sql = "delete from product_acc where id=$id limit 1";
                PdoModel::selectData($sql,[],'删除配件回收站零库存信息');
            }
        }
    }// func
 }// class